#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <time.h>

#include "GL/glut.h"
#include "Vector2D.h"
#include "ToolManager.h"

int m_windowCenter[2];
int m_windowSize[2];
int m_upKeyDepressed;
int m_downKeyDepressed;
int m_leftKeyDepressed;
int m_rightKeyDepressed;
int m_jumpKeyDepressed;
int frameCounter = 0;
float m_xyAspect;
ToolManager * toolManager;

void GlutDisplay()
{

	if(toolManager->exit)
	{
		exit(0);
	}
	glClearColor( 0,0,0, 1.0f );	
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//Draw frame here
	toolManager->PrepareFrame();
	toolManager->Draw();
	
	glutSwapBuffers();
	glutPostRedisplay();
	frameCounter++;

}

void GlutReshape(int x, int y)
{
  m_xyAspect = (float)x / (float)y;
  glViewport( 0, 0, x, y );

   glutPostRedisplay();
}

void GlutKeyboardUp(unsigned char key, int x, int y)
{
	
	int mod = glutGetModifiers();
	toolManager->SetNormalUserInput(key,mod,false);

}
void GlutKeyboard(unsigned char key, int x, int y)
{
	printf("key - %d\n", key);
	int mod = glutGetModifiers();
	toolManager->SetNormalUserInput(key, mod,true);
	
}
void GlutSpecialKeys(int key, int x, int y)
{
	printf("special key - %d\n", key);
	int mod = glutGetModifiers();
	toolManager->SetUserInput(key,mod,true);
}
void GlutSpecialKeysUp(int key, int x, int y)
{
	int mod = glutGetModifiers();
	toolManager->SetUserInput(key,mod,false);
	
}
void GlutMotion(int x, int y)
{
	Vector2D size = Vector2D(20,20);//toolManager->GetCameraSize();
	float xSize = size.x;// * 2.0;
	float ySize = size.y;// * 2.0;

	int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
	int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
	toolManager->SetActiveMouse((xSize * (float)x/(float)windowWidth), ySize - ( ySize * (float)y/(float)windowHeight));
	
}
void GlutPassiveMotion(int x, int y)
{
	Vector2D size = Vector2D(20,20);//toolManager->GetCameraSize();
	float xSize = size.x;// * 2.0;
	float ySize = size.y;// * 2.0;

	int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
	int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
	toolManager->SetPassiveMouse((xSize * (float)x/(float)windowWidth), ySize - ( ySize * (float)y/(float)windowHeight));
	
}
void GlutMouse(int button, int buttonState, int x, int y)
{
	Vector2D size = Vector2D(20,20);//toolManager->GetCameraSize();
	float xSize = size.x;// * 2.0;
	float ySize = size.y;// * 2.0;
	int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
	int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
	printf("Click (%d, %d)\n", x, y);
	toolManager->SetUserInput(button, buttonState == GLUT_DOWN, (xSize * (float)x/(float)windowWidth), ySize - ( ySize * (float)y/(float)windowHeight));

}
void Init(char * filename, char * dataFile)
{
	if(filename != NULL)
		toolManager = new ToolManager(filename, dataFile);
	
}
int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("Usage SkeletonBuilder <textureDataFile> <outputFile>\n");
		return 0;	
	}
	m_windowSize[0] = 512;
	m_windowSize[1] = 512;
	m_windowCenter[0] = m_windowSize[0]/2;
  	m_windowCenter[1] = m_windowSize[1]/2;
	m_upKeyDepressed = 0;
	m_downKeyDepressed = 0;
	m_leftKeyDepressed = 0;
	m_rightKeyDepressed = 0;
	m_jumpKeyDepressed = 0;
	
	glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);// | GLUT_STENCIL);
	glutInitWindowPosition( 50, 50 );
  	glutInitWindowSize(m_windowSize[0], m_windowSize[1] );
  	glutInit(&argc, argv);
  	//glutGameModeString( "640x480:32@60" );
  	//glutEnterGameMode(); 
  	glutCreateWindow("SkeletonBuilder");

 	
	glutDisplayFunc( GlutDisplay );
 	glutReshapeFunc( GlutReshape );  
 	glutKeyboardUpFunc( GlutKeyboardUp);
 	glutKeyboardFunc( GlutKeyboard );
 	glutSpecialFunc(GlutSpecialKeys);
 	glutSpecialUpFunc(GlutSpecialKeysUp);
 	glutMotionFunc( GlutMotion );
 	glutPassiveMotionFunc( GlutPassiveMotion);
 	glutMouseFunc( GlutMouse );
 	glEnable(GL_DEPTH_TEST);
 	srand(time(NULL));
 	char * filename = NULL;
 	
 	if(argc > 1)
 		filename = argv[1];
 		
 	Init(filename, argv[2]);
 	glutMainLoop();
  	return 0;
}

